﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PutawayRuleBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IPutawayRuleDetailDA putawayDetailRuleDA = DataAccess.CreatePutawayRuleDetailDA();
        private static readonly IPutawayRuleDA putawayRuleDA = DataAccess.CreatePutawayRule();

        public PutawayRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public PutawayRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PutawayRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<PutawayRuleInfo> putawayRuleInfos = new List<PutawayRuleInfo>();
                PutawayRuleInfo putawayRuleInfo = null;
                foreach (string id in IDS)
                {
                    putawayRuleInfo = putawayRuleDA.GetPutawayRuleByID(dataBase, tran, id, this.dataBaseID[0]);
                    putawayRuleInfo.PutawayRuleDetailInfo = putawayDetailRuleDA.GetPutawayRuleDetailListByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    putawayRuleInfos.Add(putawayRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                putawayDetailRuleDA.DeletePutawayRuleDetail(dataBase, tran, IDS, this.dataBaseID);
                putawayRuleDA.DeletePutawayRule(dataBase, tran, IDS, this.dataBaseID);
                for (int i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<PutawayRuleInfo>(dataBase, tran, putawayRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PUTAWAY_RULE", this.dataBaseID[0]);
                    if ((putawayRuleInfos[i].PutawayRuleDetailInfo != null) && (putawayRuleInfos[i].PutawayRuleDetailInfo.Count > 0))
                    {
                        for (int j = 0; j < putawayRuleInfos[i].PutawayRuleDetailInfo.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<PutawayRuleDetailInfo>(dataBase, tran, putawayRuleInfos[i].PutawayRuleDetailInfo[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "PUTAWAY_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return putawayRuleDA.GetAllPutawayRule(this.dataBaseID[0]);
        }

        public object GetInfoByID(string putawayRuleID)
        {
            PutawayRuleInfo putawayRule = putawayRuleDA.GetPutawayRuleByID(putawayRuleID, this.dataBaseID[0]);
            if (putawayRule != null)
            {
                putawayRule.PutawayRuleDetailInfo = putawayDetailRuleDA.GetPutawayRuleDetailListByID(putawayRuleID, this.dataBaseID[0], this.pubParasInfo);
            }
            return putawayRule;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            PutawayRuleQueryEntity putawayRule = obj as PutawayRuleQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(putawayRule.PutawayRuleID))
            {
                sqlWhere.Add(" PUTAWAY_RULE_ID " + putawayRule.PutawayRuleID);
            }
            if (!string.IsNullOrEmpty(putawayRule.Descr))
            {
                sqlWhere.Add(" DESCR " + putawayRule.Descr);
            }
            if (!string.IsNullOrEmpty(putawayRule.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + putawayRule.IsActive);
            }
            return putawayRuleDA.GetPutawayRuleByQueryList(sqlWhere, putawayRule, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string putawayRuleID = string.Empty;
            try
            {
                PutawayRuleInfo putawayRule = obj as PutawayRuleInfo;
                if (putawayRuleDA.CheckPutawayRuleIDUnique(dataBase, tran, putawayRule.PutawayRuleID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2410_001", this.pubParasInfo.UserLanguage, new List<string> { putawayRule.PutawayRuleID });
                }
                putawayRule.UpdatedBy = putawayRule.CreatedBy;
                putawayRule.CreatedDate = DateTime.Now;
                putawayRule.UpdatedDate = DateTime.Now;
                putawayRuleDA.InsertPutawayRule(dataBase, tran, putawayRule, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<PutawayRuleInfo>(dataBase, tran, null, putawayRule, AuditAction.Insert, this.pubParasInfo.UserID, "PUTAWAY_RULE", this.dataBaseID[0]);
                putawayRuleID = putawayRule.PutawayRuleID;
                if ((putawayRule.PutawayRuleDetailInfo != null) && (putawayRule.PutawayRuleDetailInfo.Count > 0))
                {
                    for (int i = 0; i < putawayRule.PutawayRuleDetailInfo.Count; i++)
                    {
                        if (putawayDetailRuleDA.CheckPutawayRuleDetailIDUnique(dataBase, tran, putawayRule.PutawayRuleDetailInfo[i].PutawayRuleID, putawayRule.PutawayRuleDetailInfo[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2410_002", this.pubParasInfo.UserLanguage, new List<string> { putawayRule.PutawayRuleDetailInfo[i].PutawayRuleID, putawayRule.PutawayRuleDetailInfo[i].LineID.ToString() });
                        }
                        else
                        {
                            putawayRule.PutawayRuleDetailInfo[i].CreatedDate = DateTime.Now;
                            putawayRule.PutawayRuleDetailInfo[i].UpdatedDate = DateTime.Now;
                            putawayRule.PutawayRuleDetailInfo[i].CreatedBy = this.pubParasInfo.UserID;
                            putawayRule.PutawayRuleDetailInfo[i].UpdatedBy = this.pubParasInfo.UserID;
                            putawayDetailRuleDA.InsertPutawayRuleDetail(dataBase, tran, putawayRule.PutawayRuleDetailInfo[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<PutawayRuleDetailInfo>(dataBase, tran, null, putawayRule.PutawayRuleDetailInfo[i], AuditAction.Insert, this.pubParasInfo.UserID, "PUTAWAY_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return putawayRuleID;
        }

        public string Update(object obj)
        {
            string putawayRuleID = string.Empty;
            try
            {
                PutawayRuleInfo putawayRule = obj as PutawayRuleInfo;
                PutawayRuleInfo oldPutawayRuleInfo = putawayRuleDA.GetPutawayRuleByID(putawayRule.PutawayRuleID, this.dataBaseID[0]);
                if (putawayRuleDA.UpdatePutawayRule(putawayRule, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PutawayRuleInfo>(oldPutawayRuleInfo, putawayRule, AuditAction.Update, this.pubParasInfo.UserID, "PUTAWAY_RULE", this.dataBaseID[0]);
                    putawayRuleID = putawayRule.PutawayRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return putawayRuleID;
        }
    }
}

